package com.parking.admin.action;

import com.parking.admin.entity.Parking;
import com.parking.admin.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by hujianjun on 2015/11/9.
 */
@RestController
@RequestMapping("/api/parking")
public class ParkingController {

    @Autowired
    private ParkingService parkingService;

    @RequestMapping("/find")
    public Page<Parking> find(Pageable pageable, String key) {
        return parkingService.findAll(pageable, key);
    }


    @RequestMapping("/findAll")
    public List<Parking> findAll(String key) {
           return parkingService.findAll();
    }

    @RequestMapping("/detail")
    public Parking detail(long idParking) {
        return parkingService.findOne(idParking);
    }
    
    public String getUser(HttpServletRequest request) {
        if(request.getParameter("user")!=null)
            return request.getParameter("user");
        Cookie[] cookies= request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("user"))
                return cookie.getValue();
        }
        return null;
    }

//    @RequestMapping("/save")
//    public int save(HttpServletRequest request, long idParking, String lng, String lat) {
//        return parkingService.saveLngLat(getUser(request), idParking, lng, lat);
//    }

    @RequestMapping("/save")
    @ResponseBody
    public int save(Parking parking) {
        return parkingService.saveLngLat(parking.getId(), parking.getLng(), parking.getLat());
    }

    @RequestMapping("/saveAll")
    @ResponseBody
    public int saveAll(Parking parking) {
        if(parking == null) {
            return 0;
        }
        return parkingService.saveAll((long)parking.getId(),(String)parking.getChangeLevel(),(String)parking.getChangeAddress(),(String)parking.getChangeLng(),(String)parking.getChangeLat());
    }
}
